blog

Home / DeveloperSection / Blogs / what is qlc in mnesia ?

what is qlc in mnesia ?

Sunil Singh 4203 14-Sep-2015

qlc stands for Query List Comprehension used in Query Interface to Mnesia, ETS, Dets table by using qlc we can join multiple table and fetch data .


The qlc module implements a query interface to QLC tables. Typical QLC tables are ETS, Dets, and Mnesia tables.

The main features of QLC when used with Mnesia are as follows-

  • QLC can optimize the query compiler for the Mnesia DBMS, essentially making the DBMS more efficient
  • QLC can be used as a database programming language for Mnesia

It includes a notation called "list
comprehensions" and can be used to make complex database queries over a set of tables.

The qlc module supports fast join of two query handles. Fast join is possible if some position P1 of one query handler and some position P2 of another query handler are tested for equality.

 Two fast join methods have been implemented:
  • Lookup join traverses all objects of one query handle and finds objects of the other handle (a QLC table) such that the values at P1 and P2 match or compare equal. The qlc module does not create any indices but looks up values using the key position and the indexed positions of the QLC table.
  •  Merge join sorts the objects of each query handle if necessary and filters out objects where the values at P1 and P2 do not compare equal. If there are many objects with the same value of P2 a temporary file will be used for the equivalence classes.
  • Example of qlc query -
-module(qlc_test).
-compile(export_all).

-record(person,
{id,name
}).
-record(person_detail,
{p_id,address,contactno
}).
create_table()->
mnesia:create_schema([node()]),
mnesia:start(),
mnesia:create_table(person,
[{type, set},
{disc_copies,[node()]},
{attributes, record_info(fields, person)}]),
mnesia:create_table(person_detail,
[{type, set},
{disc_copies,[node()]},
{attributes, record_info(fields, person_detail)}]).

insert_person_record(Key,Name,Address,ContactNo)->

mnesia:start(),
Fun = fun() ->
Person=#person{id=Key,name=Name},
mnesia:write(Person)


end,
Fun1 = fun() ->
PDetail=#person_detail{p_id=Key,address=Address,contactno=ContactNo},
mnesia:write(PDetail)


end,
mnesia:transaction(Fun),
mnesia:transaction(Fun1).



get_person_record_by_qlc(List)->
QHandle = qlc:q( [P||
P <- mnesia:table(person),N<-List,P#person.id==N]
),

Person = mnesia:transaction(
fun() -> qlc:e(
qlc:q( [{P#person.id,P#person.name,A#person_detail.address, A#person_detail.contactno} ||
P <- QHandle,
A <- mnesia:table(person_detail),
A#person.id == P#person_detail.p_id])
)
end
),
[Person].

get_person_record_by_qlc take one argument list type which contains person id ,in my case output looks like this.


(test@localhost)15> qlc_test:get_person_record_by_qlc([1,2,3]).
[{atomic,[{1,"person1","city1","9090099071"},
{2,"person2","city2","9090099072"},
{3,"person3","city3","9090099073"}]}]

Updated 13-Mar-2018

Leave Comment

Comments

Liked By